package comgame.scenes;

import com.almasb.fxgl.app.scene.FXGLScene;
import com.almasb.fxgl.cutscene.Cutscene;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.scene.SubScene;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import javafx.animation.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;

import java.util.List;
import java.util.Objects;

public class StartAnimatedScene extends SubScene
{
    private StackPane sceneRoot;

    public StartAnimatedScene() {
        initUI();
    }

    protected void initUI() {
        sceneRoot = new StackPane();
        sceneRoot.setPrefSize(800, 600);
        sceneRoot.setStyle("-fx-background-color: transparent;");

        // 添加背景图片
        Image backgroundImg;
        try {
            backgroundImg = new Image(Objects.requireNonNull(
                    getClass().getResourceAsStream("/assets/textures/background.png")));
        } catch (Exception e) {
            System.err.println("背景图片加载异常: " + e.getMessage());
            backgroundImg = null;
        }

        if (backgroundImg != null) {
            ImageView background = new ImageView(backgroundImg);
            background.setFitWidth(1000);
            background.setFitHeight(750);

            // 背景动画
            TranslateTransition moveTransition = new TranslateTransition(Duration.seconds(10), background);
            moveTransition.setFromX(0);
            moveTransition.setToX(-50);
            moveTransition.setCycleCount(TranslateTransition.INDEFINITE);
            moveTransition.setAutoReverse(true);
            moveTransition.play();

            sceneRoot.getChildren().add(background);
        }

        // 创建第一句文字（左上角）
        Text text1 = new Text("这是一个关于异世界的故事");
        text1.setFont(Font.font("SimHei", 40)); // 增大字体
        text1.setFill(Color.WHITE);
        text1.setStroke(Color.BLACK);
        text1.setStrokeWidth(1);
        text1.setTranslateX(30);  // 左上角偏移
        text1.setTranslateY(-220);
        text1.setOpacity(0); // 初始透明

        // 创建第二句文字（同一位置）
        Text text2 = new Text("你的爸妈被吸入传送门中");
        text2.setFont(Font.font("SimHei", 40));
        text2.setFill(Color.WHITE);
        text2.setStroke(Color.BLACK);
        text2.setStrokeWidth(1);
        text2.setTranslateX(30);  // 与第一句同位置
        text2.setTranslateY(-220);
        text2.setOpacity(0); // 初始透明

        // 创建第三句文字（左上角）
        Text text3 = new Text("你得努力种田打怪才能见到他们");
        text3.setFont(Font.font("SimHei", 40)); // 增大字体
        text3.setFill(Color.WHITE);
        text3.setStroke(Color.BLACK);
        text3.setStrokeWidth(1);
        text3.setTranslateX(30);  // 左上角偏移
        text3.setTranslateY(-220);
        text3.setOpacity(0); // 初始透明

        // 创建第四句文字（左上角）
        Text text4 = new Text("所以现在开始种田吧！！！！！");
        text4.setFont(Font.font("SimHei", 40)); // 增大字体
        text4.setFill(Color.WHITE);
        text4.setStroke(Color.BLACK);
        text4.setStrokeWidth(1);
        text4.setTranslateX(30);  // 左上角偏移
        text4.setTranslateY(-220);
        text4.setOpacity(0); // 初始透明

        // 第一句动画：淡入1秒 → 停留1秒 → 淡出1秒（总3秒）
        FadeTransition fadeIn1 = new FadeTransition(Duration.seconds(1), text1);
        fadeIn1.setFromValue(0);
        fadeIn1.setToValue(1);

        PauseTransition pause1 = new PauseTransition(Duration.seconds(1));

        FadeTransition fadeOut1 = new FadeTransition(Duration.seconds(1), text1);
        fadeOut1.setFromValue(1);
        fadeOut1.setToValue(0);

        // 第二句动画：淡入1秒 → 停留1秒 → 淡出1秒（总3秒）
        FadeTransition fadeIn2 = new FadeTransition(Duration.seconds(1), text2);
        fadeIn2.setFromValue(0);
        fadeIn2.setToValue(1);

        PauseTransition pause2 = new PauseTransition(Duration.seconds(1));

        FadeTransition fadeOut2 = new FadeTransition(Duration.seconds(1), text2);
        fadeOut2.setFromValue(1);
        fadeOut2.setToValue(0);

        // 第三句动画：淡入1秒 → 停留1秒 → 淡出1秒（总3秒）
        FadeTransition fadeIn3 = new FadeTransition(Duration.seconds(1), text3);
        fadeIn3.setFromValue(0);
        fadeIn3.setToValue(1);

        PauseTransition pause3 = new PauseTransition(Duration.seconds(1));

        FadeTransition fadeOut3 = new FadeTransition(Duration.seconds(1), text3);
        fadeOut3.setFromValue(1);
        fadeOut3.setToValue(0);

        // 第四句动画：淡入1秒 → 停留1秒 → 淡出1秒（总3秒）
        FadeTransition fadeIn4 = new FadeTransition(Duration.seconds(1), text4);
        fadeIn4.setFromValue(0);
        fadeIn4.setToValue(1);

        PauseTransition pause4 = new PauseTransition(Duration.seconds(1));

        FadeTransition fadeOut4 = new FadeTransition(Duration.seconds(1), text4);
        fadeOut4.setFromValue(1);
        fadeOut4.setToValue(0);
        // 组合顺序：第一句完成后自动播放第二句
        SequentialTransition sequence = new SequentialTransition(
                fadeIn1,    // 第一句淡入
                pause1,     // 第一句停留
                fadeOut1,   // 第一句淡出
                fadeIn2,    // 第二句淡入
                pause2,     // 第二句停留
                fadeOut2,   // 第二句淡出
                fadeIn3,    // 第三句淡入
                pause3,     // 第三句停留
                fadeOut3,   // 第三句淡出
                fadeIn4,    // 第四句淡入
                pause4,     // 第四句停留
                fadeOut4  // 第四句淡出
        );

        //开始
        sequence.setOnFinished(e -> {

            Rectangle rectangle = new Rectangle(900,700,Color.BLACK);
            Text text = new Text("爸妈! 不要!");
            text.setFill(Color.WHITE);


            ScaleTransition scaleTransition = new ScaleTransition(Duration.seconds(0.8), text);
            scaleTransition.setFromX(0);
            scaleTransition.setFromY(0);
            scaleTransition.setToX(10);
            scaleTransition.setToY(10);

            scaleTransition.play();

            sceneRoot.getChildren().clear();
            sceneRoot.getChildren().addAll(rectangle, text);


            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image HomeImg;
                try {
                    HomeImg = new Image(Objects.requireNonNull(
                            getClass().getResourceAsStream("/assets/textures/homescene.png")));
                } catch (Exception s) {
                    System.err.println("背景图片加载异常: " + s.getMessage());
                    HomeImg = null;
                }
                if ( HomeImg!= null) {
                    ImageView Homebackground = new ImageView(HomeImg);
                    Homebackground.setFitWidth(1000);
                    Homebackground.setFitHeight(750);
                    sceneRoot.getChildren().add(Homebackground);
                }
                List<String> startLines = FXGL.getAssetLoader().loadText("start.txt");
                Cutscene Startcutscene = new Cutscene(startLines);

               FXGL.getCutsceneService().startCutscene(Startcutscene);
            }, Duration.seconds(0.9));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image GameSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_GameScene.png")),900,700,false,true);
                ImageView GameScenebackground = new ImageView(GameSceneImg);
                sceneRoot.getChildren().add(GameScenebackground);
                List<String> GameToFarmLines = FXGL.getAssetLoader().loadText("GameToFarm.txt");
                Cutscene GameToFarmcutscene = new Cutscene(GameToFarmLines);
                FXGL.getCutsceneService().startCutscene(GameToFarmcutscene);
            }, Duration.seconds(1.1));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image FarmSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_Farm.png")),900,700,false,true);
                ImageView FarmScenebackground = new ImageView(FarmSceneImg);
                sceneRoot.getChildren().add(FarmScenebackground);
                List<String> FarmLines = FXGL.getAssetLoader().loadText("Farm.txt");
                Cutscene Farmcutscene = new Cutscene(FarmLines);
                FXGL.getCutsceneService().startCutscene(Farmcutscene,()->{
                });
            }, Duration.seconds(1.4));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image GameSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_GameScene.png")),900,700,false,true);
                ImageView GameScenebackground = new ImageView(GameSceneImg);
                sceneRoot.getChildren().add(GameScenebackground);
            }, Duration.seconds(1.6));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image FishPoolSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_FishPool.png")),900,700,false,true);
                ImageView FishPoolScenebackground = new ImageView(FishPoolSceneImg);
                sceneRoot.getChildren().add(FishPoolScenebackground);
                List<String> FishPoolLines = FXGL.getAssetLoader().loadText("FishPool.txt");
                Cutscene FishPoolcutscene = new Cutscene(FishPoolLines);
                FXGL.getCutsceneService().startCutscene(FishPoolcutscene);
            }, Duration.seconds(1.8));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image GameSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_GameScene.png")),900,700,false,true);
                ImageView GameScenebackground = new ImageView(GameSceneImg);
                sceneRoot.getChildren().add(GameScenebackground);
            }, Duration.seconds(2));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image StoreSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_Store.png")),900,700,false,true);
                ImageView StoreScenebackground = new ImageView(StoreSceneImg);
                sceneRoot.getChildren().add(StoreScenebackground);
                List<String> StoreLines = FXGL.getAssetLoader().loadText("Store.txt");
                Cutscene Storecutscene = new Cutscene(StoreLines);
                FXGL.getCutsceneService().startCutscene(Storecutscene);
            }, Duration.seconds(2.2));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image GameSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_GameScene.png")),900,700,false,true);
                ImageView GameScenebackground = new ImageView(GameSceneImg);
                sceneRoot.getChildren().add(GameScenebackground);
            }, Duration.seconds(2.4));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image MapSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_Map.png")),900,700,false,true);
                ImageView MapScenebackground = new ImageView(MapSceneImg);
                sceneRoot.getChildren().add(MapScenebackground);
                List<String> MapLines = FXGL.getAssetLoader().loadText("Map.txt");
                Cutscene Mapcutscene = new Cutscene(MapLines);
                FXGL.getCutsceneService().startCutscene(Mapcutscene);
            }, Duration.seconds(2.6));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image FishingSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_Fishing.png")),900,700,false,true);
                ImageView FishingScenebackground = new ImageView(FishingSceneImg);
                sceneRoot.getChildren().add(FishingScenebackground);
                List<String> FishingLines = FXGL.getAssetLoader().loadText("Fishing.txt");
                Cutscene Fishingcutscene = new Cutscene(FishingLines);
                FXGL.getCutsceneService().startCutscene(Fishingcutscene);
            }, Duration.seconds(2.8));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image MapSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_Map.png")),900,700,false,true);
                ImageView MapScenebackground = new ImageView(MapSceneImg);
                sceneRoot.getChildren().add(MapScenebackground);
            }, Duration.seconds(3));

            FXGL.runOnce(()->{
                sceneRoot.getChildren().clear();
                Image AltarSceneImg = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/Guide_Altar.png")),900,700,false,true);
                ImageView AltarScenebackground = new ImageView(AltarSceneImg);
                sceneRoot.getChildren().add(AltarScenebackground);
                List<String> AltarLines = FXGL.getAssetLoader().loadText("Altar.txt");
                Cutscene Altarcutscene = new Cutscene(AltarLines);
                FXGL.getCutsceneService().startCutscene(Altarcutscene,()->{
                    ChangeToGameScene();
                });
            }, Duration.seconds(3.2));

        });
        // 添加文字到场景
        sceneRoot.getChildren().addAll(text1, text2, text3,text4);
        sequence.play();
        getContentRoot().getChildren().add(sceneRoot);


    }

    public StackPane getSceneRoot() {
        return sceneRoot;
    }

    public void ChangeToGameScene() {
        // 1. 清除当前菜单场景的所有UI元素
        FXGL.getGameScene().clearUINodes();

        // 2. 创建GameScene实例
        GameScene gameScene = new GameScene();

        // 3. 获取新场景的根节点并添加到游戏中
        FXGL.getGameScene().addUINode(gameScene.getSceneRoot());

        // 可选：添加场景切换动画（淡入效果）
        FadeTransition fadeIn = new FadeTransition(Duration.seconds(0.5), gameScene.getSceneRoot());
        fadeIn.setFromValue(0); // 初始完全透明
        fadeIn.setToValue(1);   // 最终完全显示
        fadeIn.play();
    }
}